package code.wrtp.packets;

import code.exceptions.BadPduFormatException;
import code.wrtp.FrameControlType;

public class CoordinatorLeavingAlert implements Packet {

	private byte FC;
	private short RA;
	private short broadcast;
	private short SA;
	private short predecessorAddress;
	private short successorAddress;

	public CoordinatorLeavingAlert() {

	}

	public CoordinatorLeavingAlert(byte FC, short RA, short broadcast,
			short SA, short predecessorAddress, short successorAddress) {
		this.FC = FC;
		this.RA = RA;
		this.broadcast = broadcast;
		this.SA = SA;
		this.predecessorAddress = predecessorAddress;
		this.successorAddress = successorAddress;
	}

	public byte getFC() {
		return FC;
	}

	public short getRA() {
		return RA;
	}

	public short getBroadcast() {
		return broadcast;
	}

	public short getSA() {
		return SA;
	}

	public short getPredecessorAddress() {
		return predecessorAddress;
	}

	public short getSuccessorAddress() {
		return successorAddress;
	}

	public String toString() {
		return FC + ";" + RA + ";" + broadcast + ";" + SA + ";"
				+ predecessorAddress + ";" + successorAddress;
	}

	@Override
	public byte[] toBytes() {
		return toString().getBytes();
	}

	@Override
	public void ParseBytes(byte[] pduRaw) throws BadPduFormatException {
		
		String[] splittedPacket = new String(pduRaw).split(";", 6);
		if (splittedPacket.length != 6) {
			throw new BadPduFormatException("Wrong packet format!");
		}

		FC = Byte.parseByte(splittedPacket[0]);
		if (FC != FrameControlType.COORDINATOR_LEAVING_ALERT) {
			throw new BadPduFormatException("Wrong packet format!");
		}
		RA = Short.parseShort(splittedPacket[1]);
		broadcast = Short.parseShort(splittedPacket[2]);
		SA = Short.parseShort(splittedPacket[3]);
		predecessorAddress = Short.parseShort(splittedPacket[4]);
		successorAddress = Short.parseShort(splittedPacket[5]);
	}
}